package model

import (
	"kpt-dtm/model/yyn"
	"strconv"
)

type Formulation struct {
	Id             int64  `json:"id"`
	PastureId      int64  `json:"pasture_id"`
	RecipeId       int64  `json:"recipe_id"`
	RecipeName     string `json:"recipe_name"`
	IngredientId   int64  `json:"ingredient_id"`
	IngredientName string `json:"ingredient_name"`
	IsPremix       int8   `json:"is_premix"`
	DmPercTot      int64  `json:"dm_perc_tot"`
	CreatedAt      int64  `json:"created_at"`
	UpdatedAt      int64  `json:"updated_at"`
}

func (f *Formulation) TableName() string {
	return "d_formulation"
}

func NewFormulation(pastureId int64, recipeId, ingredientId int64, recipeName, ingredientName string, dmPercTot int64, isPreMix int8) *Formulation {
	return &Formulation{
		PastureId:      pastureId,
		RecipeId:       recipeId,
		RecipeName:     recipeName,
		IngredientId:   ingredientId,
		IngredientName: ingredientName,
		DmPercTot:      dmPercTot,
		IsPremix:       isPreMix,
	}
}

func NewFormulationList(pastureId int64, res []*yyn.FormulationData) []*Formulation {
	var formulations = make([]*Formulation, 0)
	for _, v := range res {
		recipeIdInt, _ := strconv.ParseInt(v.RecipeId, 10, 64)
		ingIdInt, _ := strconv.ParseInt(v.IngId, 10, 64)

		var isPreMix int8 = 1
		if v.IsPreMix == false {
			isPreMix = 2
		}

		DmPercTot := int64(v.DmPercTot * 100)
		formulations = append(formulations, NewFormulation(pastureId, recipeIdInt, ingIdInt, v.RecipeName, v.IngName, DmPercTot, isPreMix))
	}
	return formulations

}
